<?php
/**
 * User: zhangyu
 * Date: 2022/3/17
 * Time: 8:47
 */

declare (strict_types = 1);


namespace app\middleware;
use app\Request;
use think\Response;

/**
 * 跨域中间件
 * Class AllowOriginMiddleware
 * @package app\middleware
 */
class AllowOriginMiddleware implements  MiddlewareInterface
{
    public function handle(Request $request, \Closure $next){

        header('Access-Control-Allow-Origin: *');

        header('Access-Control-Max-Age: 1800');

        header('Access-Control-Expose-Headers: Authorization');

        header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');

        header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With, Token');

        if (strtoupper($request->method()) == "OPTIONS") {

            return Response::create()->send();

        }
        return $next($request);
    }
}
